package sri.proiect.controllers;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import sri.proiect.models.Hotel;
import sri.proiect.search.BasicSearcher;


/**
 * This servlet is used to deal with the search request
 * and return the search results to the client
 */
public class SearchController extends HttpServlet{
    
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
                      throws IOException, ServletException{
    	String searchWord = request.getParameter("searchWord");
    	
    	BasicSearcher searchManager = BasicSearcher.instance();
    	List<Hotel> searchResult = searchManager.search(searchWord);
    	
    	RequestDispatcher dispatcher = request.getRequestDispatcher("search.jsp");
    	request.setAttribute("searchResult",searchResult);
    	request.getSession().setAttribute("hotels", searchResult);
    	dispatcher.forward(request, response);
    }
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                     throws IOException, ServletException{
        doPost(request, response);
    }
}
